package com.hero.tweaks.ui.activities;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.hero.tweaks.R;
import com.hero.tweaks.util.Carousel;
import com.hero.tweaks.util.Constants;
import com.hero.tweaks.util.Helpers;
import com.htc.configuration.HtcWrapConfiguration;
import com.htc.util.HtcCommonUtil;
import com.htc.widget.ActionBarContainer;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;
import android.app.AlertDialog;

public class MainActivity extends Activity implements Constants {

	public final static String AUTHORITY = "com.hero.tweaks.activities.MainActivity";

	/*
	 * HTC Sense
	 */
	private Carousel mCarousel = null;
	private AlertDialog mAlertDialog;

	/*
	 * Sense ActionBar
	 */
	String romversion;
	public static ActionBarText mActionText;
	public static String AppTitle = "";
	static Window window;

	/*
	 * Fields
	 */
	SharedPreferences mPreferences;
	protected Fragment mFrag;
	private boolean mFirstRun = true;

	private Context context;

	/*
	 * Check whether the pro app is installed
	 */
	private boolean appInstalledOrNot(String uri) {
		PackageManager pm = getPackageManager();
		boolean app_installed = false;
		try {
			pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
			app_installed = true;
		} catch (PackageManager.NameNotFoundException e) {
			app_installed = false;
		}
		return app_installed;
	}

	/*
	 * Set the initial launch
	 */
	public boolean isFirstRun() {
		return mFirstRun;
	}

	/*
	 * Check whether the launcher icon is enabled
	 */
	public boolean isLauncherIconEnabled() {
		PackageManager p = getPackageManager();
		int componentStatus = p.getComponentEnabledSetting(new ComponentName(
				this, LauncherActivity.class));
		return componentStatus != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		super.onCreate(savedInstanceState);
		
		// setup the actionbar
		context = this;
		SetupActionBar();

		/*
		 * Now let's check to see if the pro version of the Tweaks app is
		 * installed
		 */
		boolean installed = appInstalledOrNot("com.hero.tweaks.pro");
		if (installed) {
			System.out.println("You're the real deal!");
		} else {
			showDonationDialog();
			System.out.println("Pro key not found!");
		}

		/*
		 * Here we are initializing the carousel that will house all of our main
		 * tabs.
		 */

		final int rootId = 1;
		FrameLayout viewRoot = new FrameLayout(this);
		viewRoot.setId(rootId);
		setContentView(viewRoot);
		mCarousel = new Carousel();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.add(rootId, mCarousel);
		ft.commit();

	      registerForContextMenu(viewRoot);
	      getWindow().setFormat(1);

		/*
		 * Extra step to prevent kanging. We will check the build.prop to ensure
		 * ro.product.version has the ROM name. If it doesn't, quit the app.
		 */
		
		if (Helpers.getROMVersion().contains("Renovate")) {
			Log.i(TAG, "Running on Renovate");
		} else {
			Log.e(TAG, "Incompatible ROM!");
			finish();
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add("ContextMenu");
	}

	/*
	 * Before, we checked to see whether the launcher icon was enabled. Now we
	 * want to set the visibility of the icon in the app drawer
	 */
	public void setLauncherIconEnabled(boolean enabled) {
		PackageManager p = getPackageManager();
		int newState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
				: PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
		p.setComponentEnabledSetting(new ComponentName(this,
				LauncherActivity.class), newState, PackageManager.DONT_KILL_APP);
	}

	/*
	 * Here we will set up the parameters for the HTC actionbar
	 */
	private void SetupActionBar() {
		ActionBarExt localActionBarExt = new ActionBarExt(this, getActionBar());
		localActionBarExt.setFullScreenEnabled(true);
		ActionBarContainer localActionBarContainer = localActionBarExt
				.getCustomContainer();
		mActionText = new ActionBarText(this);
		mActionText.setPrimaryText(AppTitle);
		AppTitle = Helpers.getROMVersion();
		boolean installed = appInstalledOrNot("com.hero.tweaks.pro");
		if (Helpers.getROMVersion().contains("Renovate")) {
			if (installed) {
				mActionText.setPrimaryText(AppTitle + " Pro");
			} else {
				mActionText.setPrimaryText(AppTitle);
			}
		}
		localActionBarContainer.addCenterView(mActionText);
	}

	/*
	 * We will use this method to create a timeout dialog that disappears only
	 * if the pro key is found on the device. Please do not remove this! :)
	 */
	private void showDonationDialog() {
		this.mAlertDialog = new AlertDialog.Builder(this)
				.setTitle("Get PRO")
				.setMessage(
						"Consider purchasing a pro key to support development!")
				.setPositiveButton("Get PRO!",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(
									DialogInterface paramAnonymousDialogInterface,
									int paramAnonymousInt) {
								Uri uri = Uri
										.parse("https://play.google.com/store/apps/details?id=com.hero.tweaks.pro");
								Intent intent = new Intent(Intent.ACTION_VIEW,
										uri);
								startActivity(intent);
								finish();
							}
						})
				.setNegativeButton("Not now",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(
									DialogInterface paramAnonymousDialogInterface,
									int paramAnonymousInt) {
							}
						}).setCancelable(false).create();
		this.mAlertDialog.show();
		this.mAlertDialog.getButton(-2).setEnabled(true);
		this.mAlertDialog.getButton(-1).setEnabled(true);
		this.mAlertDialog.setCanceledOnTouchOutside(false);
	}
}